package com.vcredit.creditcat.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.vo.RedisDO;
import com.vcredit.creditcat.vo.RedisResultVo;

@Service
public class RedisManageService {
	@Value("${redis.manage.url}")
	private String REDIS_MANAGE_URL;
	private Integer REDIS_PORT = 6379;

	public Page<RedisResultVo> queryAllKey() {
		List<RedisResultVo> list = new ArrayList<RedisResultVo>();
		RedisDO rd = new RedisDO();
		rd.open(REDIS_MANAGE_URL, REDIS_PORT);
		Set<String> s = rd.jedis.keys("*");
		Iterator<String> it = s.iterator();

		while (it.hasNext()) {
			String key = (String) it.next();
			list.add(new RedisResultVo(key));
		}
		rd.close();
		return new Page<RedisResultVo>((long) list.size(), list);
	}

	public void delete(String keys) {
		RedisDO rd = new RedisDO();
		rd.open(REDIS_MANAGE_URL, REDIS_PORT);
		for (String key : keys.split(",")) {
			rd.jedis.del(key);
		}
		rd.close();
	}

//	public void removeAll() {
//		RedisDO rd = new RedisDO();
//		rd.open(REDIS_MANAGE_URL, REDIS_PORT);
//		rd.returnResource(rd.jedis);
//		rd.close();
//	}
}
